/**
 * Created by LeungZ on 2017/3/8.
 */
/**
 * Created by LeungZ on 2016/10/17.
 */
class Ctrl {
    constructor($scope, $filter) {
        "ngInject";
        this._broadcast = $scope.$broadcast.bind($scope);
        this._dateFilter = $filter('date');

        this.atsMinDate = new Date();
        this.atsSpaceOpts = [{val: '001', desc: '一次'}, {val: '002', desc: '每天'}, {val: '003', desc: '每周'},
            {val: '004', desc: '每月'}, {val: '005', desc: '每年'}];
        this.dayOfWeek = ['周日', '周一', '周二', '周三', '周四', '周五', '周六'];

        if (!$scope.service.atsSpace) $scope.service.atsSpace = '001';

        if ($scope.service.atsStartTime && $scope.service.atsSpace) {
            $scope.$evalAsync(()=> {
                this.spaceChange($scope.service);
            });
            if ($scope.service.atsEndTime) this.setSpaceInst($scope.service)
        }

        $scope.$watch('service.atsStartTime', (newVal, oldVal)=> {
            if (newVal !== oldVal) {
                this.atsStartDayOfWeek = this.dayOfWeek[new Date(+newVal).getDay()];
                if ($scope.service.atsEndTime < newVal) {
                    $scope.service.atsEndTime = null;
                }
                this.setSpaceInst($scope.service);
                $scope.$applyAsync(()=> {
                    this.spaceChange($scope.service);
                });
            }
        });
        $scope.$watch('service.atsEndTime', (newVal, oldVal)=> {
            if (newVal !== oldVal) {
                this.atsEndDayOfWeek = this.dayOfWeek[new Date(+newVal).getDay()];
                if (this.atsMaxDate && this.atsMaxDate < newVal) {
                    $scope.$applyAsync(()=> {
                        $scope.service.atsEndTime = this.atsMaxDate;
                    });
                } else {
                    this.setSpaceInst($scope.service);
                }
            }
        });
    }

    setSpaceInst(ser) {
        if (ser.atsStartTime && ser.atsEndTime) {
            var startDate = new Date(+ser.atsStartTime);
            var endDate = new Date(+ser.atsEndTime);
            switch (ser.atsSpace) {
                case "001":
                    this.atsSpaceInst = '从 ' + this._dateFilter(startDate, 'yyyy年MM月dd日 HH时mm分ss')
                        + ' 至 ' + this._dateFilter(endDate, 'yyyy年MM月dd日 HH时mm分ss');
                    break;
                case "002":
                    this.atsSpaceInst = this._dateFilter(startDate, 'HH时mm分ss')
                        + (endDate.getDate() > startDate.getDate() ? ' 至次日 ' : ' 至 ')
                        + this._dateFilter(endDate, 'HH时mm分ss') + '. '
                        + '从 ' + this._dateFilter(startDate, 'yyyy年MM月dd日') + '开始';
                    break;
                case "003":
                    this.atsSpaceInst = this.dayOfWeek[startDate.getDay()] + ' '
                        + this._dateFilter(startDate, 'HH时mm分ss')
                        + ((endDate.getDay() <= startDate.getDay()
                        && endDate.getDate() != startDate.getDate()) ? ' 至次周 ' : ' 至 ')
                        + this.dayOfWeek[endDate.getDay()] + ' ' + this._dateFilter(endDate, 'HH时mm分ss') + '. '
                        + '从 ' + this._dateFilter(startDate, 'yyyy年MM月dd日') + '开始';
                    break;
                case "004":
                    this.atsSpaceInst = '第 ' + startDate.getDate() + ' 日 ' + this._dateFilter(startDate, 'HH时mm分ss')
                        + (endDate.getMonth() > startDate.getMonth() ? ' 至次月第 ' : ' 至第 ')
                        + endDate.getDate() + ' 日 ' + this._dateFilter(endDate, 'HH时mm分ss') + '. '
                        + '从 ' + this._dateFilter(startDate, 'yyyy年MM月') + '开始';
                    break;
                case "005":
                    this.atsSpaceInst = this._dateFilter(startDate, 'MM月dd日 HH时mm分ss')
                        + (endDate.getYear() > startDate.getYear() ? ' 至次年 ' : ' 至 ')
                        + this._dateFilter(endDate, 'MM月dd日 HH时mm分ss') + '. '
                        + '从 ' + this._dateFilter(startDate, 'yyyy年') + '开始';
                    break;
            }
        } else this.atsSpaceInst = '';

    }

    spaceChange(ser) {
        if (ser.atsStartTime) {
            switch (ser.atsSpace) {
                case "002":
                    this.atsMaxDate = +ser.atsStartTime + 86400000;
                    break;
                case "003":
                    this.atsMaxDate = +ser.atsStartTime + 604800000;
                    break;
                case "004":
                    this.atsMaxDate = new Date(+ser.atsStartTime);
                    this.atsMaxDate.setMonth(this.atsMaxDate.getMonth() + 1);
                    this.atsMaxDate = this.atsMaxDate.getTime();
                    break;
                case "005":
                    this.atsMaxDate = new Date(+ser.atsStartTime);
                    this.atsMaxDate.setFullYear(this.atsMaxDate.getFullYear() + 1);
                    this.atsMaxDate = this.atsMaxDate.getTime();
                    break;
                case "001":
                    this.atsMaxDate = 9999999999999;
                    break;
            }
            if (ser.atsEndTime && this.atsMaxDate < ser.atsEndTime) {
                ser.atsEndTime = this.atsMaxDate;
            }
            this._broadcast('pickerUpdate', 'endTime', {
                minDate: new Date(+ser.atsStartTime),
                maxDate: new Date(+this.atsMaxDate)
            });
        }
    }
}
export default Ctrl